package com.stoa.floatlib

import android.app.Application
import android.util.Log
import com.stoa.floatlib.constant.StoaFloatConstant
import com.stoa.floatlib.core.*
import com.stoa.floatlib.core.LastStoaViewPosInfo
import com.stoa.floatlib.core.NormalStoaFloatViewManager
import com.stoa.floatlib.core.StoaFloatManagerInterface
import com.stoa.floatlib.core.StoaFloatViewManager
import com.stoa.floatlib.core.SysStoaFloatViewManager

object StoaFloatReal {
    internal val managerMap = mutableMapOf<String, StoaFloatViewManager>()
    internal val lifecycleMap = mutableMapOf<String, StoaFloatManagerInterface>()
    internal val mLastPosInfoMaps = mutableMapOf<String, LastStoaViewPosInfo>()

    private fun hasTagManager(viewTag: String): Boolean {
        return managerMap.keys.contains(viewTag)
    }

    fun show(viewTag: String) {
        showMainIcon(viewTag)
    }

    fun showMainIcon(viewTag: String) {
        if (StoaFloat.mContext == null) {
            Log.e(StoaFloat.TAG, "请先调用StoaFloat的init方法！！")
            return
        }
        if (!hasTagManager(viewTag)) {
            managerMap[viewTag] = StoaFloatViewManager(StoaFloat.mContext!!)
            lifecycleMap[viewTag] = if (StoaFloatConstant.IS_NORMAL_FLOAT_MODE) {
                NormalStoaFloatViewManager(StoaFloat.mContext!!, viewTag)
            } else {
                SysStoaFloatViewManager()
            }
        }
        StoaFloatConstant.IS_NORMAL_FLOAT_MODE = true
        managerMap[viewTag]?.attachMainIcon(viewTag)
    }

    fun init(app: Application) {
        app.registerActivityLifecycleCallbacks(StoaActivityLifecycleCallbacks())
    }

    fun hide(viewTag: String) {
        managerMap[viewTag]?.detach()
    }
}